<template>
    <div class="container">
      <!-- 固定在顶部的标题栏 -->
      <div class="header"><DesktopOutlined />后台管理系统</div>
  
      <!-- 固定在顶部的 a-alert -->
      <a-alert
        v-if="showSuccess"
        message="登录成功！"
        type="success"
        :closable="true"
        @close="handleClose"
        @afterClose="handlerAutoClose"
        show-icon
        class="fixed-alert"
      />

      <a-alert
      message="Error"
      description="账号或密码错误"
      v-if="showError"
      type="error"
      show-icon
      :closable="true"
      @close="handleClose"
      @afterClose="handlerAutoClose"
       class="fixed-alert"
    >
      <template #icon><smile-outlined /></template>
    </a-alert>
  
      <div class="content">
        <div class="form">
          <a-form
            :model="formState"
            name="basic"
            :label-col="{ span: 4 }"
            :wrapper-col="{ span: 16 }"
            autocomplete="off"
            @finish="onFinish"
            @finishFailed="onFinishFailed"
          >
            <a-form-item
              label="用户名"
              name="username"
              :rules="[{ required: true, message: '请输入用户名!' }]"
            >
              <a-input v-model:value="formState.username" />
            </a-form-item>
  
            <a-form-item
              label="密码"
              name="password"
              :rules="[{ required: true, message: '请输入密码!' }]"
            >
              <a-input-password v-model:value="formState.password" />
            </a-form-item>
  
            <a-form-item name="remember" :wrapper-col="{ offset: 8, span: 8 }">
              <a-checkbox v-model:checked="formState.remember">Remember me</a-checkbox>
            </a-form-item>
  
            <a-form-item :wrapper-col="{ offset: 8, span: 8 }">
              <a-button type="primary" html-type="submit" size="large">Submit</a-button>
            </a-form-item>
          </a-form>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { reactive, ref } from 'vue';
  import {DesktopOutlined} from '@ant-design/icons-vue'
  // 定义一个响应式变量来控制 alert 的显示
  const showError = ref(false);
  const showSuccess = ref(false)
  const formState = reactive({
    username: '',
    password: '',
    remember: true,
  });
  
  // 处理表单提交成功
  const onFinish = values => {
    const { username, password } = values;
    if (username === "admin" && password === "123456") {
        showSuccess.value = true; // 显示成功提示

      const successTimeout = setTimeout(()=>{
        showSuccess.value = false
      },500)
    } else {
        showError.value = true
        const ErrorTimeout = setTimeout(()=>{
            showError.value = false
      },500)
    }
  };
  
  // 处理表单提交失败
  const onFinishFailed = errorInfo => {
    console.log('Failed:', errorInfo);
  };
  
  // 处理关闭事件
  const handleClose = () => {
    showSuccess.value = false;
    showError.value = false
  };

  const handlerAutoClose =() =>{
    clearTimeout(ErrorTimeout)
    clearTimeout(successTimeout)
  }
  </script>
  
  <style scoped>
  /* 公共样式 */
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }
  
  body, html {
    height: 100%;
  }
  
  .container {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100vh; /* 使用vh单位让高度等于视口高度 */
    background-color: #f9f9f9;
    justify-content: center; /* 垂直居中内容 */
    align-items: center; /* 水平居中内容 */
  }
  
  .content {
    text-align: center;
    max-width: 500px; /* 设置最大宽度以适应不同屏幕尺寸 */
    width: 100%; /* 确保在小屏幕上也能占满整个宽度 */
  }
  
  .form {
    background-color: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  }
  
  /* 标题样式 */
  .header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%; /* 确保标题宽度为100% */
    background-color: black;
    color: white;
    text-align: center;
    line-height: 100px;
    font-size: 30px;
    text-decoration: dotted underline white;
    z-index: 1; /* 确保标题不会被其他元素遮挡 */
  }
  
  /* 动画定义 */
  @keyframes TranslateX {
    from {
      transform: translateX(-100vw); /* 从视口外左侧开始 */
    }
    to {
      transform: translateX(0); /* 移动到视口内 */
    }
  }
  
  /* 固定在顶部的 a-alert 样式 */
  .fixed-alert {
    position: fixed;
    top: 100px; /* 根据 header 的高度调整 */
    left: 0;
    right: 0;
    z-index: 2; /* 确保 a-alert 在其他内容之上 */
    margin: 0 auto;
    width: 80%; /* 调整宽度以适应不同屏幕尺寸 */
    max-width: 600px; /* 设置最大宽度 */
    text-align: center;
  }
  
  /* 为 .header 添加动画 */
  .header {
    animation: TranslateX 5s linear forwards; /* 动画播放一次并停留在最后一帧 */
  }
  </style>